import Footer from '@/components/Footer';
import {LockOutlined, UserOutlined,} from '@ant-design/icons';
import {LoginForm, ProFormText,} from '@ant-design/pro-components';
import {useEmotionCss} from '@ant-design/use-emotion-css';
import {Helmet, history} from '@umijs/max';
import {message, Tabs} from 'antd';
import React, {useState} from 'react';
import Settings from '../../../../config/defaultSettings';
import {userRegisterUsingPost} from "@/services/mybi/userController";
import {Link} from "@@/exports";


const Login: React.FC = () => {
    const [type, setType] = useState<string>('account');
    const containerClassName = useEmotionCss(() => {
        return {
            display: 'flex',
            flexDirection: 'column',
            height: '100vh',
            overflow: 'auto',
            backgroundImage:
                "url('https://mdn.alipayobjects.com/yuyan_qk0oxh/afts/img/V-_oS6r-i7wAAAAAAAAAAAAAFl94AQBr')",
            backgroundSize: '100% 100%',
        };
    });
    const handleSubmit = async (values: API.UserRegisterRequest) => {
        try {
            // 登录
            const res = await userRegisterUsingPost({
                ...values
            });
            if (res.code === 0) {
                const defaultLoginSuccessMessage = '注册成功！';
                message.success(defaultLoginSuccessMessage);
                history.push('/user/login');
                return;
            } else {
                message.error(res.message)
            }
        } catch (error) {
            const defaultRegisterFailureMessage = '注册失败，请重试！';
            console.log(error);
            message.error(defaultRegisterFailureMessage);
        }
    };
    return (
        <div className={containerClassName}>
            <Helmet>
                <title>
                    {'注册'}- {Settings.title}
                </title>
            </Helmet>
            <div
                style={{
                    flex: '1',
                    padding: '32px 0',
                }}
            >
                <LoginForm
                    submitter={{
                        searchConfig: {
                            submitText: '注册'
                        }
                    }}
                    contentStyle={{
                        minWidth: 280,
                        maxWidth: '75vw',
                    }}
                    logo={<img alt="logo" src="/logo.svg"/>}
                    title="智能BI"
                    subTitle={'智能BI 是结合接入AI实现的一个图表分析项目'}
                    onFinish={async (values) => {
                        await handleSubmit(values as API.UserRegisterRequest);
                    }}
                >
                    <Tabs
                        activeKey={type}
                        onChange={setType}
                        centered
                        items={[
                            {
                                key: 'account',
                                label: '账户注册',
                            },

                        ]}
                    />

                    {type === 'account' && (
                        <>
                            <ProFormText
                                name="userAccount"
                                fieldProps={{
                                    size: 'large',
                                    prefix: <UserOutlined/>,
                                }}
                                placeholder={'请输入用户名！'}
                                rules={[
                                    {
                                        required: true,
                                        message: '用户名是必填项！',
                                    },
                                ]}
                            />
                            <ProFormText.Password
                                name="userPassword"
                                fieldProps={{
                                    size: 'large',
                                    prefix: <LockOutlined/>,
                                }}
                                placeholder={'请输入密码！'}
                                rules={[
                                    {
                                        required: true,
                                        message: '密码是必填项！',
                                    },
                                ]}
                            />

                            <ProFormText.Password
                                name="checkPassword"
                                fieldProps={{
                                    size: 'large',
                                    prefix: <LockOutlined/>,
                                }}
                                placeholder={'请重新输入密码！'}
                                rules={[
                                    {
                                        required: true,
                                        message: '密码是必填项！',
                                    },
                                ]}
                            />
                        </>
                    )}

                    <div
                        style={{
                            marginBottom: 24,
                        }}
                    >
                        <Link
                            to="/user/login"
                        >
                            去登陆
                        </Link>
                    </div>

                </LoginForm>
            </div>
            <Footer/>
        </div>
    );
};
export default Login;
